#include "ws2812b.h"

uint16_t Single_LED_Buffer[DATA_SIZE*LED_NUM + 1] = {0};//比特数组
uint32_t LED_Buffer[LED_NUM] = {0};//灯数组
uint16_t ColorLight_Time = 0;//设定的灯带颜色切换时间
uint8_t Color_Light_Flag = 0;//灯带颜色切换的标志

/**
 * Function: PWM_WS2812B_Init
 * Description: ws2812b相关硬件配置
 * input param: arr:定时器3的自动重装载值
 * return param: 无
 */
void PWM_WS2812B_Init(uint16_t arr)
{
	GPIO_InitType GPIO_InitStructure;
	TMR_TimerBaseInitType TIM_TimeBaseInitStructure;
	TMR_OCInitType TIM_OCInitStructure;
	DMA_InitType DMA_InitStructure;
	NVIC_InitType NVIC_InitStructure;
	
	RCC_AHBPeriphClockCmd(RCC_AHBPERIPH_GPIOA,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_TMR1,ENABLE);
	RCC_AHBPeriphClockCmd(RCC_AHBPERIPH_DMA1,ENABLE);
	
	GPIO_PinAFConfig(GPIOA,GPIO_PinsSource11,GPIO_AF_2);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_OutType = GPIO_OutType_PP;
	GPIO_InitStructure.GPIO_Pins = GPIO_Pins_11;
	GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	TMR_Reset(TMR1);
	TMR_InternalClockConfig(TMR1);
	TIM_TimeBaseInitStructure.TMR_ClockDivision = TMR_CKD_DIV1;
	TIM_TimeBaseInitStructure.TMR_CounterMode = TMR_CounterDIR_Up;
	TIM_TimeBaseInitStructure.TMR_Period = arr-1;
	TIM_TimeBaseInitStructure.TMR_DIV = 2-1;
	TIM_TimeBaseInitStructure.TMR_RepetitionCounter = 0;
	TMR_TimeBaseInit(TMR1,&TIM_TimeBaseInitStructure);
	
	TMR_OCStructInit(&TIM_OCInitStructure);
	TIM_OCInitStructure.TMR_OCMode = TMR_OCMode_PWM1;
	TIM_OCInitStructure.TMR_OutputState = TMR_OutputState_Enable;
	TIM_OCInitStructure.TMR_Pulse = 0;
	TIM_OCInitStructure.TMR_OCPolarity = TMR_OCPolarity_High;
	TMR_OC4Init(TMR1,&TIM_OCInitStructure);
	
	TMR_CtrlPWMOutputs(TMR1, ENABLE);
	
	TMR_OC4PreloadConfig(TMR1, TMR_OCPreload_Enable); 
	TMR_DMAConfig(TMR1,TMR_DMABase_CC4,TMR_DMABurstLength_1Transfer);
	TMR_DMACmd(TMR1,TMR_DMA_CC4,ENABLE);

	TMR_Cmd(TMR1,DISABLE);
	
	DMA_Reset(DMA1_Channel4);
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(TMR1->CC4);
	DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)(Single_LED_Buffer);
	DMA_InitStructure.DMA_Direction = DMA_DIR_PERIPHERALDST;
	DMA_InitStructure.DMA_BufferSize = 24*LED_NUM +1;
	DMA_InitStructure.DMA_PeripheralInc = DMA_PERIPHERALINC_DISABLE;
	DMA_InitStructure.DMA_MemoryInc = DMA_MEMORYINC_ENABLE;
	DMA_InitStructure.DMA_PeripheralDataWidth = DMA_PERIPHERALDATAWIDTH_HALFWORD;
	DMA_InitStructure.DMA_MemoryDataWidth = DMA_MEMORYDATAWIDTH_HALFWORD;
	DMA_InitStructure.DMA_Mode = DMA_MODE_NORMAL;
	DMA_InitStructure.DMA_MTOM = DMA_MEMTOMEM_DISABLE;
	DMA_InitStructure.DMA_Priority = DMA_PRIORITY_MEDIUM;
	DMA_Init(DMA1_Channel4,&DMA_InitStructure);
	
	
	NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel7_4_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	DMA_INTConfig(DMA1_Channel4, DMA_INT_TC, ENABLE);
	
	DMA_ChannelEnable(DMA1_Channel4,DISABLE);
	
}
/**
 * Function: TIMx_Cmd
 * Description: 定时器3是否使能
 * input param: NewState:定时器的使能状态(Enable,Disable)
 * return param: 无
 */
void TIMx_Cmd(FunctionalState NewState)
{
	TMR_Cmd(TMR1, NewState);
}
/**
 * Function: TIMx_SetCompare4
 * Description: 定时器3的占空比
 * input param: Value: 占空比的比值
 * return param: 无
 */
void TIMx_SetCompare4(uint16_t Value)
{
	TMR_SetCompare4(TMR1,Value);
}
/**
 * Function: DMA1_Start
 * Description: 开启DMA1
 * input param: DataNumber: DMA传输的数据的个数
 * return param: 无
 */
void DMA_Start(uint16_t DataNumber)
{
	DMA_ChannelEnable(DMA1_Channel4,DISABLE);
	DMA_SetCurrDataCounter(DMA1_Channel4,DataNumber);
	DMA_ChannelEnable(DMA1_Channel4,ENABLE);
}
/**
 * Function: DMA1_Channel4_5_IRQHandler
 * Description: DMA1通道4和5的中断
 * input param: DataNumber: DMA传输的数据的个数
 * return param: 无
 */
uint8_t WS2812B_Flag;//DMA传输完成的标志
void DMA1_Channel7_4_IRQHandler(void)
{
	if(DMA_GetFlagStatus(DMA1_FLAG_TC4))
	{
		TIMx_SetCompare4(0);
		TIMx_Cmd(DISABLE);
		WS2812B_Flag=1;
		//DMA1_Handler();
		DMA_ClearFlag(DMA1_FLAG_TC4);
	}
}
/************************************************以上是需要配置的部分****************************************************/
/**
 * Function: WS2812B_Init
 * Description: 灯带的初始化
 * input param: 无
 * return param: 无
 */
void WS2812B_Init(void)
{
	PWM_WS2812B_Init(WS2812B_ARR);
	TIMx_SetCompare4(0);
	TIMx_Cmd(DISABLE);
	WS2812B_Flag=1;
}
/**
 * Function: WS2812B_ClearBuf
 * Description: 关灯
 * input param: 无
 * return param: 无
 */
void WS2812B_ClearBuf(void)
{
	uint8_t i;
	for(i=0;i<LED_NUM;i++)
	{
		LED_Buffer[i]=0x000000;
	}
	Single_LED_Buffer[0] = 0;
	Single_LED_Buffer[DATA_SIZE*LED_NUM] = 0;
	PWM_WS2812B_Write_24Bits();
}
/**
 * Function: PWM_WS2818B_Write_buf
 * Description: 给灯带数组内赋值
 * input param: color: 灯带的颜色
 * return param: 无
 */
void PWM_WS2818B_Write_buf(uint32_t color)
{	
	uint8_t i;
	for(i = 0;i < LED_NUM; i++)
	{
		LED_Buffer[i] = color;
	}
}
/**
 * Function: PWM_WS2812B_Write_24Bits
 * Description: 经灯带的颜色转换为对应的高低电平
 * input param: 无
 * return param: 无
 */
void PWM_WS2812B_Write_24Bits(void)
{
	uint8_t i,j;
	Single_LED_Buffer[0] = 0;
	
	for(j = 0;j < LED_NUM;j++)
	{
		for(i = 0;i<DATA_SIZE;i++)
		Single_LED_Buffer[j * 24 + i] = ((LED_Buffer[j] << i) & 0x800000)? T1H : T0H;
	}
	//Single_LED_Buffer[0] = ((LED_Buffer[0]) & 0x800000)? T1H : T0H;
	//Single_LED_Buffer[0] = 20;
	DMA_Start(24*LED_NUM +1 );
	//DMA1_Start(3);
	TIMx_Cmd(ENABLE);
	while(WS2812B_Flag==0);
	WS2812B_Flag=0;
}
/**
 * Function: WhiteLight_Brightness_0
 * Description: 白光弱等
 * input param: 颜色切换的时间
 * return param: 无
 */
void WhiteLight_Brightness_0(uint8_t time_counter)
{
	ColorLight_Time = time_counter;
	PWM_WS2818B_Write_buf(0x010101);
	PWM_WS2812B_Write_24Bits();
}
/**
 * Function: WhiteLight_Brightness_1
 * Description: 白光中等
 * input param: 颜色切换的时间
 * return param: 无
 */
void WhiteLight_Brightness_1(uint8_t time_counter)
{
	ColorLight_Time=time_counter;
	PWM_WS2818B_Write_buf(0x3F3F3F);
	PWM_WS2812B_Write_24Bits();
	
}
/**
 * Function: WhiteLight_Brightness_2
 * Description: 白光强等
 * input param: 颜色切换的时间
 * return param: 无
 */
void WhiteLight_Brightness_2(uint8_t time_counter)
{
	ColorLight_Time=time_counter;
	PWM_WS2818B_Write_buf(0xffffff);
	PWM_WS2812B_Write_24Bits();
}
/**
 * Function: Color_Light_Mode_0
 * Description: 呼吸灯（蓝，红，绿，紫，青，黄，白,橙）
 * input param: 颜色切换的时间
 * return param: 无
 */
void Color_Light_Mode_0(uint16_t time_counter, uint16_t color_mask,uint8_t brightness, uint8_t min_brightness)
{
	static uint8_t i = 0 ,Color = 0,Min_brightness = 0,Max_brightness = 0;
	static int8_t direction = 1; // 新增方向变量，1表示递增，-1表示递减
	ColorLight_Time=time_counter;
	Max_brightness = brightness;
	Min_brightness = min_brightness;

	if(color_mask & (1 << 0))
	{
		if(i==0)	PWM_WS2818B_Write_buf((Color));
		//if(i==1)	PWM_WS2818B_Write_buf((Max_brightness-Color));
		if(i==1)	PWM_WS2818B_Write_buf((Color));
	}
	if(color_mask & (1 << 1))
	{
		if(i==2)	PWM_WS2818B_Write_buf((Color)<<8);
		//if(i==3)	PWM_WS2818B_Write_buf((Max_brightness-Color)<<8);
		if(i==3)	PWM_WS2818B_Write_buf((Color)<<8);
	}
	if(color_mask & (1 << 2))
	{
		if(i==4)	PWM_WS2818B_Write_buf((Color)<<16);
		//if(i==5)	PWM_WS2818B_Write_buf((Max_brightness-Color)<<16);
		if(i==5)	PWM_WS2818B_Write_buf((Color)<<16);
	}
	if(color_mask & (1 << 3))
	{
		if(i==6)	PWM_WS2818B_Write_buf((Color)|((Color)<<8));
		//if(i==7)	PWM_WS2818B_Write_buf((Max_brightness-Color)|((Max_brightness-Color)<<8));
		if(i==7)	PWM_WS2818B_Write_buf((Color)|((Color)<<8));
	}
	if(color_mask & (1 << 4))
	{
		if(i==8)	PWM_WS2818B_Write_buf((Color)|((Color)<<16));
		//if(i==9)	PWM_WS2818B_Write_buf((Max_brightness-Color)|((Max_brightness-Color)<<16));
		if(i==9)	PWM_WS2818B_Write_buf((Color)|((Color)<<16));
	}
	if(color_mask & (1 << 5))
	{
		if(i==10)	PWM_WS2818B_Write_buf(((Color)<<8)|((Color)<<16));
		//if(i==11)	PWM_WS2818B_Write_buf(((Max_brightness-Color)<<8)|((Max_brightness-Color)<<16));
		if(i==11)	PWM_WS2818B_Write_buf(((Color)<<8)|((Color)<<16));
	}
	if(color_mask & (1 << 6))
	{
		if(i==12)	PWM_WS2818B_Write_buf(((Color))|((Color)<<8)|((Color)<<16));
		//if(i==13)	PWM_WS2818B_Write_buf(((Max_brightness-Color))|((Max_brightness-Color)<<8)|((Max_brightness-Color)<<16));
		if(i==13)	PWM_WS2818B_Write_buf(((Color))|((Color)<<8)|((Color)<<16));
	}
	if(color_mask & (1 << 7))
	{
		if(i==14)	PWM_WS2818B_Write_buf((Color<<8)|(((uint32_t)((Color*31)/Max_brightness))<<16));
		//if(i==15)	PWM_WS2818B_Write_buf(((Max_brightness-Color)<<8)|((31-((uint32_t)((Color*31)/Max_brightness)))<<16));
		if(i==15)	PWM_WS2818B_Write_buf((Color<<8)|(((uint32_t)((Color*31)/Max_brightness))<<16));
	}
	// Color++;
	// if(Color > Max_brightness)
	// {
	// 	Color = Min_brightness;
	// }
	Color += direction;
	// 边界检查，改变方向
	if (Color >= Max_brightness) {
		direction = -1;
		Color = Max_brightness; // 防止越界
	} else if (Color <= Min_brightness) {
		direction = 1;
		Color = Min_brightness; // 防止越界
	}
	if(Color==Min_brightness)
	{
		uint16_t N;
		do {
			i = (i + 1) % 16;
			N = i / 2;
		}while(!(color_mask & (1 << N)));
	}
	PWM_WS2812B_Write_24Bits();
}
/**
 * Function: Color_Light_Mode_1
 * Description: 流水灯
 * input param: 颜色切换的时间
 * return param: 无
 */
void Color_Light_Mode_1(uint8_t time_counter)//GRB
{
	uint8_t i,RandNum;
	uint32_t G,R,B;
	static uint8_t j;
	ColorLight_Time=time_counter;
	for(i=LED_NUM;i>0;i--)
	{
		LED_Buffer[i]=LED_Buffer[i-1];
	}
	
	if(j==0)
	{
		RandNum=rand()%7;
		if(RandNum==0)LED_Buffer[0]=0x0000FF;
		if(RandNum==1)LED_Buffer[0]=0x00FF00;
		if(RandNum==2)LED_Buffer[0]=0xFF0000;
		if(RandNum==3)LED_Buffer[0]=0x00FFFF;
		if(RandNum==4)LED_Buffer[0]=0xFF00FF;
		if(RandNum==5)LED_Buffer[0]=0xFFFF00;
		if(RandNum==6)LED_Buffer[0]=0xFFFFFF;
	}
	else if(j<27)
	{
		G=LED_Buffer[1]/0x10000%0x100;
		R=LED_Buffer[1]/0x100%0x100;
		B=LED_Buffer[1]%0x100;
		if(G>20)G-=10;
		if(R>20)R-=10;
		if(B>20)B-=10;
		LED_Buffer[0]=(G<<16)|(R<<8)|B;
	}
	else
	{
		LED_Buffer[0]=0;
	}
	
	j++;
	j%=50;
	PWM_WS2812B_Write_24Bits();
}
/**
 * Function: Color_Light_Mode_2
 * Description: 流水灯
 * input param: 颜色切换的时间
 * return param: 无
 */
void Color_Light_Mode_2(uint8_t time_counter)
{
	uint8_t i;
	static uint8_t j;
	ColorLight_Time=time_counter;
	for(i=LED_NUM;i>0;i--)
	{
		LED_Buffer[i]=LED_Buffer[i-1];
	}
	if(j==0)	LED_Buffer[0]=rand()%0x1000000;
	else 	LED_Buffer[0]=LED_Buffer[1];
	j++;
	j%=10;
	PWM_WS2812B_Write_24Bits();
}
/**
 * Function: Color_Light_Mode_3
 * Description: 随机色灯
 * input param: 颜色切换的时间
 * return param: 无
 */
void Color_Light_Mode_3(uint8_t time_counter)
{
	uint8_t i;
	ColorLight_Time = time_counter;
	for(i=0;i<LED_NUM;i++)
	{
		LED_Buffer[i]=rand()%0x1000000;
	}
	PWM_WS2812B_Write_24Bits();
}
/**
 * Function: Light_Mode
 * Description: 灯模式的选择
 * input param: mode: 灯的模式（范围0-6）
 * 				time_counter: 颜色切换的时间
 * 				state: 使能灯的开与关
 * return param: 无
 */
extern uint8_t Color_Light_Flag;//定时器内的使能位
void Light_Mode(uint8_t mode,uint16_t time_counter,FunctionalState state)
{
	switch(mode)
	{
		case 0:
			if(Color_Light_Flag == 1)
			{
				Color_Light_Flag = 0;
				if(state)	WhiteLight_Brightness_0(time_counter);
				else		WS2812B_ClearBuf();
			}
		break;
		case 1:
			if(Color_Light_Flag == 1)
			{
				Color_Light_Flag = 0;
				if(state)	WhiteLight_Brightness_1(time_counter);
				else		WS2812B_ClearBuf();
			}
		break;
		case 2:
			if(Color_Light_Flag == 1)
			{
				Color_Light_Flag = 0;
				if(state)	WhiteLight_Brightness_2(time_counter);
				else		WS2812B_ClearBuf();
			}
		break;
		case 3:
			if(Color_Light_Flag == 1)
			{
				Color_Light_Flag = 0;
				if(state)	Color_Light_Mode_0(time_counter,((1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7)),255,0);
				else		WS2812B_ClearBuf();
			}
		break;
		case 4:
			if(Color_Light_Flag == 1)
			{
				Color_Light_Flag = 0;
				if(state)	Color_Light_Mode_1(time_counter);
				else		WS2812B_ClearBuf();
			}
		break;
		case 5:
			if(Color_Light_Flag == 1)
			{
				Color_Light_Flag = 0;
				if(state)	Color_Light_Mode_2(time_counter);
				else		WS2812B_ClearBuf();
			}
		break;
		case 6:
			if(Color_Light_Flag == 1)
			{
				Color_Light_Flag = 0;
				if(state)	Color_Light_Mode_3(time_counter);
				else		WS2812B_ClearBuf();
			}
		break;
		default:
		break;
	}
		
}
/**
 * Function: Blue_Breathe_Light
 * Description: 蓝色呼吸灯
 * input param: time_counter: 颜色切换的时间		
 * return param: 无
 */
void Blue_Breathe_Light(uint8_t time_counter)
{
	static uint8_t i = 0 ,Color = 0;
	ColorLight_Time=time_counter;
	if(i==0)	PWM_WS2818B_Write_buf((Color));
	if(i==1)	PWM_WS2818B_Write_buf((255-Color));
	Color++;
	if(Color==0)
	{
		i+= 1;
		i%= 2;
	}
	PWM_WS2812B_Write_24Bits();
}
/**
 * Function: Red_Breathe_Light
 * Description: 红色呼吸灯
 * input param: time_counter: 颜色切换的时间		
 * return param: 无
 */
void Red_Breathe_Light(uint8_t time_counter)
{
	static uint8_t i = 0 ,Color = 0;
	ColorLight_Time=time_counter;
	if(i==0)	PWM_WS2818B_Write_buf((Color)<<8);
	if(i==1)	PWM_WS2818B_Write_buf((255-Color)<<8);
	Color++;
	if(Color==0)
	{
		i+= 1;
		i%= 2;
	}
	PWM_WS2812B_Write_24Bits();
}
/**
 * Function: Green_Breathe_Light
 * Description: 绿色呼吸灯
 * input param: time_counter: 颜色切换的时间		
 * return param: 无
 */
void Green_Breathe_Light(uint8_t time_counter)
{
	static uint8_t i = 0 ,Color = 0;
	ColorLight_Time=time_counter;
	if(i==0)	PWM_WS2818B_Write_buf((Color)<<16);
	if(i==1)	PWM_WS2818B_Write_buf((255-Color)<<16);
	Color++;
	if(Color==0)
	{
		i+= 1;
		i%= 2;
	}
	PWM_WS2812B_Write_24Bits();
}
/**
 * Function: Purple_Breathe_Light
 * Description: 紫色呼吸灯
 * input param: time_counter: 颜色切换的时间		
 * return param: 无
 */
void Purple_Breathe_Light(uint8_t time_counter)
{
	static uint8_t i = 0 ,Color = 0;
	ColorLight_Time=time_counter;
	if(i==0)	PWM_WS2818B_Write_buf((Color)|((Color)<<8));
	if(i==1)	PWM_WS2818B_Write_buf((255-Color)|((255-Color)<<8));
	Color++;
	if(Color==0)
	{
		i+= 1;
		i%= 2;
	}
	PWM_WS2812B_Write_24Bits();
}
/**
 * Function: Cyan_Breathe_Light
 * Description: 青色呼吸灯
 * input param: time_counter: 颜色切换的时间		
 * return param: 无
 */
void Cyan_Breathe_Light(uint8_t time_counter)
{
	static uint8_t i = 0 ,Color = 0;
	ColorLight_Time=time_counter;
	if(i==0)	PWM_WS2818B_Write_buf((Color)|((Color)<<16));
	if(i==1)	PWM_WS2818B_Write_buf((255-Color)|((255-Color)<<16));
	Color++;
	if(Color==0)
	{
		i+= 1;
		i%= 2;
	}
	PWM_WS2812B_Write_24Bits();
}
/**
 * Function: Yellow_Breathe_Light
 * Description: 黄色呼吸灯
 * input param: time_counter: 颜色切换的时间		
 * return param: 无
 */
void Yellow_Breathe_Light(uint8_t time_counter)
{
	static uint8_t i = 0 ,Color = 0;
	ColorLight_Time=time_counter;
	if(i==0)	PWM_WS2818B_Write_buf(((Color)<<8)|((Color)<<16));
	if(i==1)	PWM_WS2818B_Write_buf(((255-Color)<<8)|((255-Color)<<16));
	Color++;
	if(Color==0)
	{
		i+= 1;
		i%= 2;
	}
	PWM_WS2812B_Write_24Bits();
}
/**
 * Function: White_Breathe_Light
 * Description: 白色呼吸灯
 * input param: time_counter: 颜色切换的时间		
 * return param: 无
 */
void White_Breathe_Light(uint8_t time_counter)
{
	static uint8_t i = 0 ,Color = 0;
	ColorLight_Time=time_counter;
	if(i==0)	PWM_WS2818B_Write_buf(((Color))|((Color)<<8)|((Color)<<16));
	if(i==1)	PWM_WS2818B_Write_buf(((255-Color))|((255-Color)<<8)|((255-Color)<<16));
	Color++;
	if(Color==0)
	{
		i+= 1;
		i%= 2;
	}
	PWM_WS2812B_Write_24Bits();
}
/**
 * Function: Orange_Breathe_Light
 * Description: 橙色呼吸灯
 * input param: time_counter: 颜色切换的时间		
 * return param: 无
 */
void Orange_Breathe_Light(uint8_t time_counter)
{
	static uint8_t i = 0 ,Color = 0;
	ColorLight_Time=time_counter;
	if(i==0)	PWM_WS2818B_Write_buf((Color<<8)|(((uint32_t)((Color*31)/255))<<16));
	if(i==1)	PWM_WS2818B_Write_buf(((255-Color)<<8)|((31-((uint32_t)((Color*31)/255)))<<16));
	Color++;
	if(Color==0)
	{
		i+= 1;
		i%= 2;
	}
	PWM_WS2812B_Write_24Bits();
}
/**
 * Function: Work_Breathe_Mode
 * Description: 分开颜色的呼吸灯
 * input param: mode: 灯的模式（0:蓝，1:红，2:绿，3:紫，4:青，5:黄，6:白，7:橙）
 * 				time_counter: 颜色切换的时间
 * 				state: 使能灯的开与关
 * return param: 无
 */
void Work_Breathe_Mode(uint8_t mode,uint16_t time_counter,FunctionalState state)
{
	if(state)
	{
		switch(mode)
		{
			case 0:
				if(Color_Light_Flag == 1)
				{
					Color_Light_Flag = 0;
					Blue_Breathe_Light(time_counter);
				}
				break;
			case 1:
				if(Color_Light_Flag == 1)
				{
					Color_Light_Flag = 0;
					Red_Breathe_Light(time_counter);
				}
				break;
			case 2:
				if(Color_Light_Flag == 1)
				{
					Color_Light_Flag = 0;
					Green_Breathe_Light(time_counter);
				}
				break;
			case 3:
				if(Color_Light_Flag == 1)
				{
					Color_Light_Flag = 0;
					Purple_Breathe_Light(time_counter);
				}
				break;
			case 4:
				if(Color_Light_Flag == 1)
				{
					Color_Light_Flag = 0;
					Cyan_Breathe_Light(time_counter);
				}
				break;
			case 5:
				if(Color_Light_Flag == 1)
				{
					Color_Light_Flag = 0;
					Yellow_Breathe_Light(time_counter);
				}
				break;
			case 6:
				if(Color_Light_Flag == 1)
				{
					Color_Light_Flag = 0;
					White_Breathe_Light(time_counter);
				}
				break;
			case 7:
				if(Color_Light_Flag == 1)
				{
					Color_Light_Flag = 0;
					Orange_Breathe_Light(time_counter);
				}
				break;	
			default :
				break;	
		}
	}
	else	WS2812B_ClearBuf();
}
/**
 * Function: Colour_Brightness_Choose_Mode
 * Description: 灯的颜色，亮度，模式选择
 * input param: mode: 灯的模式（0:蓝，1:红，2:绿，3:紫，4:青，5:黄，6:白，7:橙）
 * 				brightness: 灯的亮度
 * 				state: 使能灯的开与关
 * return param: 无
 */
void Colour_Brightness_Choose_Mode(uint8_t mode,uint8_t brightness,FunctionalState state)
{
	if(state)
	{
		ColorLight_Time=20;
		switch(mode)
		{
			case (1 << 0):
				if(Color_Light_Flag == 1)
				{
					Color_Light_Flag = 0;
					PWM_WS2818B_Write_buf((brightness));
					PWM_WS2812B_Write_24Bits();
				}
				break;
			case (1 << 1):
				if(Color_Light_Flag == 1)
				{
					Color_Light_Flag = 0;
					PWM_WS2818B_Write_buf((brightness) << 8);
					PWM_WS2812B_Write_24Bits();
				}
				break;
			case (1 << 2):
				if(Color_Light_Flag == 1)
				{
					Color_Light_Flag = 0;
					PWM_WS2818B_Write_buf((brightness) << 16);
					PWM_WS2812B_Write_24Bits();
				}
				break;
			case (1 << 3):
				if(Color_Light_Flag == 1)
				{
					Color_Light_Flag = 0;
					PWM_WS2818B_Write_buf((brightness) | ((brightness) << 8));
					PWM_WS2812B_Write_24Bits();
				}
				break;
			case (1 << 4):
				if(Color_Light_Flag == 1)
				{
					Color_Light_Flag = 0;
					PWM_WS2818B_Write_buf((brightness) | ((brightness) << 16));
					PWM_WS2812B_Write_24Bits();
				}
				break;
			case (1 << 5):
				if(Color_Light_Flag == 1)
				{
					Color_Light_Flag = 0;
					PWM_WS2818B_Write_buf(((brightness)<<8) | ((brightness) << 16));
					PWM_WS2812B_Write_24Bits();
				}
				break;
			case (1 << 6):
				if(Color_Light_Flag == 1)
				{
					Color_Light_Flag = 0;
					PWM_WS2818B_Write_buf(((brightness)) | ((brightness) << 8)|((brightness) << 16));
					PWM_WS2812B_Write_24Bits();
				}
				break;
			case (1 << 7):
				if(Color_Light_Flag == 1)
				{
					Color_Light_Flag = 0;
					PWM_WS2818B_Write_buf((brightness<<8) | (((uint32_t)((brightness*31) / 255)) << 16));
					PWM_WS2812B_Write_24Bits();
				}
				break;
			default:
				break;
		}
		
	}
	else WS2812B_ClearBuf();
}
/**
 * Function: Ws2812b_Color
 * Description: 灯带颜色的选择
 * input param: mode: 灯的模式（0:单色光，1:呼吸灯，2:其他）
 * 				{模式为0的时候：只用写下述三个参数（不包括mode)，time_counter参数会被忽略
 * 				color_mask: 灯的颜色掩码(蓝色：0 << 0 红色：1 << 0 绿色：2 << 0 紫色：3 << 0 青色：4 << 0 黄色：5 << 0 白色：6 << 0 橙色：7 << 0)
 * 				brightness: 灯的亮度(0-255)
 * 				state: 使能灯的开与关}
 *  			{模式为1的时候:
 * 				color_mask: 灯的颜色掩码(蓝色：0 << 0 红色：1 << 0 绿色：2 << 0 紫色：3 << 0 青色：4 << 0 黄色：5 << 0 白色：6 << 0 橙色：7 << 0)
 * 				brightness: 灯的亮度上限(0-255)	
 * 				time_counter: 颜色切换的时间
 * 				state: 使能灯的开与关}
 * 				{模式为2的时候: }
 * return param: 无
 */
uint16_t color_mask;
void Ws2812b_Color(uint8_t mode,uint16_t color_mask, uint8_t brightness,uint8_t min_brightness, uint16_t time_counter,FunctionalState state)
{
	switch(mode)
	{
		case 0:
			Colour_Brightness_Choose_Mode(color_mask,brightness,state);
			break;
		case 1:
			if(Color_Light_Flag == 1)
			{
				Color_Light_Flag = 0;
				if(state)
					Color_Light_Mode_0(time_counter, color_mask,brightness,min_brightness);
				else
					WS2812B_ClearBuf();
			}
			break;
		case 2:

			break;
		default:
			break;
	}
}

/**
 * Function: Deal_Time_Set
 * Description: 处理时间设置
 * input param: 无
 * return param: 无
 */
void Deal_Time_Set(void)
{
	static uint16_t count = 0;
	count++;
	if(count >= ColorLight_Time)
	{
		count = 0;
		Color_Light_Flag = 1;
	}	
}


